package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    ClazzService clazzService;

    //班级分页查询
    @GetMapping
    public Result pageString(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             String name,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("分页查询，参数：{},{},{},{},{}", page, pageSize, name, begin, end);
        PageBean pageBean = clazzService.pageString(page, pageSize, name, begin, end);
        return Result.ok(pageBean);
    }

    //删除班级
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除班级:{}", id);
        clazzService.deleteById(id);
        return Result.ok();
    }

    //添加班级
    @PostMapping
    public Result insert(@RequestBody Clazz clazz) {
        log.info("添加班级:{}", clazz);
        clazzService.insert(clazz);
        return Result.ok();
    }

    //根据ID查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询:{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.ok(clazz);
    }
    //修改班级
    @PutMapping
    public Result updateById(@RequestBody Clazz clazz) {
        log.info("修改班级:{}", clazz);
        clazzService.updateById(clazz);
        return Result.ok();
    }

    //查询所有班级
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有班级");
        List<Clazz> list = clazzService.list();
        return Result.ok(list);
    }
}
